<script>
export default {
  data() {
    return {
      videoFeedSrc: '',
      videoFeedKey: 0,
      isModalVisible: false,
      selectedVideo: null,
      isProcessing: false,
      connectionStatus: 'disconnected' // disconnected, connecting, connected
    };
  },
  methods: {
    showUploadModal() {
      this.isModalVisible = true;
    },
    closeModal() {
      this.isModalVisible = false;
      this.videoFeedSrc = '';
      this.selectedVideo = null;
      this.isProcessing = false;
    },
    startCamera() {
      this.stopAll();
      this.connectionStatus = 'connecting';
      this.videoFeedSrc = '/camera_feed';

      // 模拟连接过程
      setTimeout(() => {
        this.connectionStatus = 'connected';
      }, 1500);
    },
    stopAll() {
      this.videoFeedSrc = '';
      this.connectionStatus = 'disconnected';
      this.isProcessing = false;
    },
    onFileChange(event) {
      this.selectedVideo = event.target.files[0];
    },
    async startUpload() {
      if (!this.selectedVideo) {
        this.$message.warning("请选择一个视频文件");
        return;
      }

      this.isProcessing = true;
      const formData = new FormData();
      formData.append('video', this.selectedVideo);

      try {
        const response = await fetch('/upload_video', {
          method: 'POST',
          body: formData
        });

        const data = await response.json();

        if (data.status === 'success') {
          this.videoFeedSrc = '';
          this.$nextTick(() => {
            this.videoFeedSrc = '/video_feed';
            this.videoFeedKey += 1;
          });
          this.closeModal();
          this.$message.success('视频上传成功，开始分析');
        } else {
          this.$message.error('上传失败');
        }
      } catch (error) {
        console.error('Error:', error);
        this.$message.error('上传失败');
      } finally {
        this.isProcessing = false;
      }
    }
  }
};
</script>

<template>
  <div class="monitoring-container">
    <!-- 头部标题区域 -->
    <div class="header-section">
      <div class="title-wrapper">
        <div class="title-icon">
          <svg viewBox="0 0 24 24" fill="currentColor">
            <path
              d="M12 2C13.1 2 14 2.9 14 4C14 5.1 13.1 6 12 6C10.9 6 10 5.1 10 4C10 2.9 10.9 2 12 2ZM21 9V7L15 1H5C3.89 1 3 1.89 3 3V21C3 22.11 3.89 23 5 23H19C20.11 23 21 22.11 21 21V9M19 21H5V3H13V9H19Z" />
          </svg>
        </div>
        <h1 class="main-title">智能交通监控系统</h1>
        <div class="status-indicator" :class="connectionStatus">
          <span class="status-dot"></span>
          <span class="status-text">
            {{ connectionStatus === 'connected' ? '已连接' :
              connectionStatus === 'connecting' ? '连接中...' : '未连接' }}
          </span>
        </div>
      </div>
    </div>

    <!-- 控制面板 -->
    <div class="control-panel">
      <div class="control-buttons">
        <button class="control-btn camera-btn" @click="startCamera" :disabled="connectionStatus === 'connecting'">
          <div class="btn-icon">
            <svg viewBox="0 0 24 24" fill="currentColor">
              <path
                d="M17,10.5V7A1,1 0 0,0 16,6H4A1,1 0 0,0 3,7V17A1,1 0 0,0 4,18H16A1,1 0 0,0 17,17V13.5L21,17.5V6.5L17,10.5Z" />
            </svg>
          </div>
          <span>实时监控</span>
          <div class="btn-loading" v-if="connectionStatus === 'connecting'"></div>
        </button>

        <button class="control-btn upload-btn" @click="showUploadModal" :disabled="isProcessing">
          <div class="btn-icon">
            <svg viewBox="0 0 24 24" fill="currentColor">
              <path d="M14,2H6A2,2 0 0,0 4,4V20A2,2 0 0,0 6,22H18A2,2 0 0,0 20,20V8L14,2M18,20H6V4H13V9H18V20Z" />
            </svg>
          </div>
          <span>视频分析</span>
          <div class="btn-loading" v-if="isProcessing"></div>
        </button>

        <button class="control-btn stop-btn" @click="stopAll" :disabled="!videoFeedSrc">
          <div class="btn-icon">
            <svg viewBox="0 0 24 24" fill="currentColor">
              <path d="M18,18H6V6H18V18Z" />
            </svg>
          </div>
          <span>停止</span>
        </button>
      </div>
    </div>

    <!-- 视频显示区域 -->
    <div class="video-section">
      <div class="video-container" :class="{ 'has-video': videoFeedSrc }">
        <div v-if="!videoFeedSrc" class="video-placeholder">
          <div class="placeholder-icon">
            <svg viewBox="0 0 24 24" fill="currentColor">
              <path
                d="M17,10.5V7A1,1 0 0,0 16,6H4A1,1 0 0,0 3,7V17A1,1 0 0,0 4,18H16A1,1 0 0,0 17,17V13.5L21,17.5V6.5L17,10.5Z" />
            </svg>
          </div>
          <h3>等待视频输入</h3>
          <p>请选择实时监控或上传视频文件进行分析</p>
        </div>

        <div v-else class="video-wrapper">
          <img :src="videoFeedSrc" :key="videoFeedKey" class="video-display" @error="stopAll" />
          <div class="video-overlay">
            <div class="video-info">
              <span class="live-indicator" v-if="connectionStatus === 'connected'">
                <span class="live-dot"></span>
                LIVE
              </span>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 上传视频模态框 -->
    <div v-if="isModalVisible" class="modal-overlay" @click="closeModal">
      <div class="modal-content" @click.stop>
        <div class="modal-header">
          <h3>上传视频文件</h3>
          <button class="close-btn" @click="closeModal">
            <svg viewBox="0 0 24 24" fill="currentColor">
              <path
                d="M19,6.41L17.59,5L12,10.59L6.41,5L5,6.41L10.59,12L5,17.59L6.41,19L12,13.41L17.59,19L19,17.59L13.41,12L19,6.41Z" />
            </svg>
          </button>
        </div>

        <div class="modal-body">
          <div class="upload-area" :class="{ 'has-file': selectedVideo }">
            <input type="file" id="videoFileInput" name="video" accept="video/*" required @change="onFileChange"
              class="file-input">
            <label for="videoFileInput" class="upload-label">
              <div class="upload-icon">
                <svg viewBox="0 0 24 24" fill="currentColor">
                  <path d="M14,2H6A2,2 0 0,0 4,4V20A2,2 0 0,0 6,22H18A2,2 0 0,0 20,20V8L14,2M18,20H6V4H13V9H18V20Z" />
                </svg>
              </div>
              <div class="upload-text">
                <span v-if="!selectedVideo">点击选择视频文件</span>
                <span v-else class="file-name">{{ selectedVideo.name }}</span>
              </div>
              <div class="upload-hint">支持 MP4, AVI, MOV 格式</div>
            </label>
          </div>
        </div>

        <div class="modal-footer">
          <button class="modal-btn cancel-btn" @click="closeModal">取消</button>
          <button class="modal-btn confirm-btn" @click="startUpload" :disabled="!selectedVideo || isProcessing">
            <span v-if="!isProcessing">开始分析</span>
            <span v-else>分析中...</span>
            <div class="btn-loading" v-if="isProcessing"></div>
          </button>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.monitoring-container {
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 20px;
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}

/* 头部区域 */
.header-section {
  margin-bottom: 30px;
}

.title-wrapper {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 15px;
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(10px);
  border-radius: 20px;
  padding: 20px 40px;
  border: 1px solid rgba(255, 255, 255, 0.2);
}

.title-icon {
  width: 40px;
  height: 40px;
  color: #fff;
}

.main-title {
  color: #fff;
  font-size: 2.5rem;
  font-weight: 700;
  margin: 0;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
}

.status-indicator {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 16px;
  border-radius: 20px;
  background: rgba(255, 255, 255, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.2);
}

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #ff4757;
  animation: pulse 2s infinite;
}

.status-indicator.connected .status-dot {
  background: #2ed573;
}

.status-indicator.connecting .status-dot {
  background: #ffa502;
}

.status-text {
  color: #fff;
  font-size: 0.9rem;
  font-weight: 500;
}

/* 控制面板 */
.control-panel {
  margin-bottom: 30px;
}

.control-buttons {
  display: flex;
  justify-content: center;
  gap: 20px;
  flex-wrap: wrap;
}

.control-btn {
  position: relative;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 16px 32px;
  border: none;
  border-radius: 15px;
  font-size: 1.1rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.2);
  min-width: 160px;
  justify-content: center;
}

.control-btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.camera-btn {
  background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
  color: #fff;
  box-shadow: 0 8px 25px rgba(79, 172, 254, 0.3);
}

.camera-btn:hover:not(:disabled) {
  transform: translateY(-2px);
  box-shadow: 0 12px 35px rgba(79, 172, 254, 0.4);
}

.upload-btn {
  background: linear-gradient(135deg, #fa709a 0%, #fee140 100%);
  color: #fff;
  box-shadow: 0 8px 25px rgba(250, 112, 154, 0.3);
}

.upload-btn:hover:not(:disabled) {
  transform: translateY(-2px);
  box-shadow: 0 12px 35px rgba(250, 112, 154, 0.4);
}

.stop-btn {
  background: linear-gradient(135deg, #ff6b6b 0%, #ee5a24 100%);
  color: #fff;
  box-shadow: 0 8px 25px rgba(255, 107, 107, 0.3);
}

.stop-btn:hover:not(:disabled) {
  transform: translateY(-2px);
  box-shadow: 0 12px 35px rgba(255, 107, 107, 0.4);
}

.btn-icon {
  width: 24px;
  height: 24px;
}

.btn-loading {
  width: 16px;
  height: 16px;
  border: 2px solid rgba(255, 255, 255, 0.3);
  border-top: 2px solid #fff;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin-left: 8px;
}

/* 视频区域 */
.video-section {
  display: flex;
  justify-content: center;
}

.video-container {
  width: 100%;
  max-width: 1200px;
  min-height: 600px;
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(10px);
  border-radius: 20px;
  border: 1px solid rgba(255, 255, 255, 0.2);
  overflow: hidden;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
}

.video-placeholder {
  text-align: center;
  color: rgba(255, 255, 255, 0.8);
  padding: 60px 40px;
}

.placeholder-icon {
  width: 80px;
  height: 80px;
  margin: 0 auto 20px;
  opacity: 0.6;
}

.video-placeholder h3 {
  font-size: 1.8rem;
  margin: 0 0 10px;
  font-weight: 600;
}

.video-placeholder p {
  font-size: 1.1rem;
  margin: 0;
  opacity: 0.8;
}

.video-wrapper {
  position: relative;
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.video-display {
  max-width: 100%;
  max-height: 100%;
  border-radius: 15px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
}

.video-overlay {
  position: absolute;
  top: 20px;
  left: 20px;
  right: 20px;
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  pointer-events: none;
}

.live-indicator {
  display: flex;
  align-items: center;
  gap: 8px;
  background: rgba(255, 0, 0, 0.9);
  color: #fff;
  padding: 8px 16px;
  border-radius: 20px;
  font-weight: 600;
  font-size: 0.9rem;
}

.live-dot {
  width: 8px;
  height: 8px;
  background: #fff;
  border-radius: 50%;
  animation: pulse 1s infinite;
}

/* 模态框 */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.7);
  backdrop-filter: blur(5px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  padding: 20px;
}

.modal-content {
  background: #fff;
  border-radius: 20px;
  width: 100%;
  max-width: 500px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
  overflow: hidden;
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 25px 30px;
  border-bottom: 1px solid #eee;
}

.modal-header h3 {
  margin: 0;
  font-size: 1.5rem;
  font-weight: 600;
  color: #333;
}

.close-btn {
  background: none;
  border: none;
  width: 30px;
  height: 30px;
  cursor: pointer;
  color: #666;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s ease;
}

.close-btn:hover {
  background: #f5f5f5;
  color: #333;
}

.modal-body {
  padding: 30px;
}

.upload-area {
  border: 2px dashed #ddd;
  border-radius: 15px;
  padding: 40px 20px;
  text-align: center;
  transition: all 0.3s ease;
  cursor: pointer;
}

.upload-area:hover {
  border-color: #4facfe;
  background: rgba(79, 172, 254, 0.05);
}

.upload-area.has-file {
  border-color: #2ed573;
  background: rgba(46, 213, 115, 0.05);
}

.file-input {
  display: none;
}

.upload-label {
  cursor: pointer;
  display: block;
}

.upload-icon {
  width: 50px;
  height: 50px;
  margin: 0 auto 15px;
  color: #666;
}

.upload-text {
  font-size: 1.2rem;
  font-weight: 600;
  color: #333;
  margin-bottom: 8px;
}

.file-name {
  color: #2ed573;
}

.upload-hint {
  font-size: 0.9rem;
  color: #666;
}

.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 15px;
  padding: 25px 30px;
  border-top: 1px solid #eee;
  background: #f9f9f9;
}

.modal-btn {
  padding: 12px 24px;
  border: none;
  border-radius: 10px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  gap: 8px;
}

.cancel-btn {
  background: #f5f5f5;
  color: #666;
}

.cancel-btn:hover {
  background: #e9e9e9;
}

.confirm-btn {
  background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
  color: #fff;
}

.confirm-btn:hover:not(:disabled) {
  transform: translateY(-1px);
  box-shadow: 0 5px 15px rgba(79, 172, 254, 0.4);
}

.confirm-btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

/* 动画 */
@keyframes pulse {

  0%,
  100% {
    opacity: 1;
  }

  50% {
    opacity: 0.5;
  }
}

@keyframes spin {
  0% {
    transform: rotate(0deg);
  }

  100% {
    transform: rotate(360deg);
  }
}

/* 响应式设计 */
@media (max-width: 768px) {
  .monitoring-container {
    padding: 15px;
  }

  .main-title {
    font-size: 2rem;
  }

  .title-wrapper {
    flex-direction: column;
    gap: 10px;
    padding: 20px;
  }

  .control-buttons {
    flex-direction: column;
    align-items: center;
  }

  .control-btn {
    width: 100%;
    max-width: 300px;
  }

  .video-container {
    min-height: 400px;
  }

  .modal-content {
    margin: 20px;
  }
}
</style>